﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Account
// Project Description :    
// ===============================================================================
// Class Name          :    ActUserBargain4cashRepository
// Namespace:          :    Gfw.Repositories.Account
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 10:20:09
// Update Time         :    2012/6/24 10:20:09
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Repository;
using Gfw.Models.Account;
using NHibernate.Criterion;

namespace Gfw.Repositories.Account
{
    public class ActUserBargain4cashRepository : Repository<ActUserBargain4cash>
    {
        /// <summary>
        /// 根据用户ID与交易时间查询用户现金币交易情况
        /// <para>主要功能：修改相关信息（如：审核通过 | 审核不通过）</para>
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="tradeDate">交易时间</param>
        /// <param name="unitOfWork"></param>
        /// <returns></returns>
        public ActUserBargain4cash GetUserCashBargainByUserIDAndTradeDate(string userID, DateTime tradeDate, UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
            {
                var query = Session.CreateCriteria("ActUserBargain4cash").Add(Expression.Eq("Userid", userID))
                    .Add(Expression.Eq("TradeDate", tradeDate));

                return query.List<ActUserBargain4cash>().FirstOrDefault();
            }
            else
            {
                var query = unitOfWork.Session.CreateCriteria("ActUserBargain4cash").Add(Expression.Eq("Userid", userID))
                    .Add(Expression.Eq("TradeDate", tradeDate));

                return query.List<ActUserBargain4cash>().FirstOrDefault();
            }
        }

        public virtual IList<ActUserBargain4cash> Query(string userId, int[] tradeTypes, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalPage)
        {
            var query = Session.CreateCriteria("ActUserBargain4cash").Add(Expression.Eq("Userid", userId));
            if (start.HasValue)
                query = query.Add(Expression.Ge("TradeDate", start.Value));
            if (end.HasValue)
            {
                end = end.Value.AddDays(1);
                query = query.Add(Expression.Le("TradeDate", end.Value));
            }

            if(tradeTypes != null)
                query = query.Add(Expression.In("TradeType", tradeTypes));

            query = query.AddOrder(new Order("TradeDate", false));

            totalPage = (query.List().Count + pageSize - 1) / pageSize;
            return query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActUserBargain4cash>();
        }

        /// <summary>
        /// 增加收入记录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <param name="amount"></param>
        /// <param name="balance"></param>
        /// <param name="unitOfWork"></param>
        public virtual void AddIncome(string userId, int type, decimal amount, decimal balance, string awardSerialNo,
            UnitOfWork unitOfWork = null)
        {
            ActUserBargain4cash bargain = new ActUserBargain4cash()
            {
                Id = Guid.NewGuid().ToString(),
                Userid = userId,
                TradeType = type,
                Income = amount,
                Outlay = 0,
                Balance = balance,
                TradeDate = DateTime.Now,
                AwardSerialNo = awardSerialNo,
            };

            Save(bargain, unitOfWork);
        }


        /// <summary>
        /// 增加支出记录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <param name="amount"></param>
        /// <param name="balance"></param>
        /// <param name="unitOfWork"></param>
        public virtual void AddOutlay(string userId, int type, decimal amount, decimal balance, string awardSerialNo, UnitOfWork unitOfWork = null)
        {
            ActUserBargain4cash bargain = new ActUserBargain4cash()
            {
                Id = Guid.NewGuid().ToString(),
                Userid = userId,
                TradeType = type,
                Income = 0,
                Outlay = amount,
                Balance = balance,
                TradeDate = DateTime.Now,
                AwardSerialNo = awardSerialNo
            };

            Save(bargain, unitOfWork);
        }
    }
}
